starter.controller('checkDetailController', ['ApiEndpoint','localStore','$rootScope','$timeout','$ionicPopup','$state', '$scope', 'Page',
    function(ApiEndpoint,localStore,$rootScope,$timeout,$ionicPopup,$state, $scope, Page) {


        var selectItem = localStore.getObject("selectItem");
        $scope.screenWidth = document.body.clientWidth;
        $scope.checkResult = {};
        $scope.selectItem = selectItem;
        $scope.checkResult.result = "1";

        Page.ajaxPostQuiet(ApiEndpoint.checkTaskImgsUrl, {itemId : selectItem.itemId,taskId : selectItem.taskId}, function (resp) {
            $scope.checkResult.files = resp.data;
        });

        //if(selectItem.path != undefined || selectItem.path != "") {
        //    $scope.checkResult.file = {};
        //    $scope.checkResult.file.path = selectItem.path;
        //    $scope.checkResult.file.name = selectItem.name;
        //    $scope.checkResult.file.type = "jpg";
        //}

        if(selectItem.comment != undefined || selectItem.comment != "") {
            $scope.checkResult.comment = selectItem.comment;
        }


        Page.ajaxPostQuiet(ApiEndpoint.mobileCheckDetailUrl, {itemId : selectItem.itemId,taskId : selectItem.taskId}, function (resp) {
            for (var i = 0; i < resp.data.length; i++) {
                var obj = resp.data[i];
                if(obj.templateValue == undefined || obj.templateValue == '') {
                    obj.checkTrue = true;
                } else {
                    if(obj.templateValue == '是') {
                        obj.checkTrue = true;
                    } else {
                        obj.checkTrue = false;
                    }
                }

            }
            $scope.checkDetail = angular.extend([],resp.data);
        });


        //$scope.resCheckSubmit = function() {
        //    console.log($scope);
        //};

        $scope.clickTrue = function(detail,$event) {
            if($event.currentTarget.checked) {
                detail.checkTrue = true;
            }
        };

        $scope.clickFalse = function(detail, $event) {
            if($event.currentTarget.checked) {
                detail.checkTrue = false;
            }
        };

        //$scope.$watch('checkResult.files', function (newValue, oldValue, $scope) {
        //    if(newValue != undefined) {
        //        if(newValue.type != "jpg" && newValue.type != "jpeg" && newValue.type != "png"
        //            && newValue.type != "bmp" && newValue.type != "gif") {
        //            //window.webkit.messageHandlers.mobileAlert.postMessage({title:'警告',content:"请上传图片",action:false})
        //            var alertPopup = $ionicPopup.alert({
        //                title: '警告',
        //                template: '请选择图片类型的文件!'
        //            });
        //            return;
        //        }
        //    }
        //});
        $scope.confirmClick = function() {
            var confirmPopup = $ionicPopup.confirm({
                title: '删除图片',
                template: '<span style="margin-left: 28%">您确定要删除吗?</span>',
                cancelText : '取消',
                okText : '确定'
            });
            confirmPopup.then(function(res) {
                if(res) {
                    $scope.checkResult.files.splice($scope.tempIndex,1);
                    $scope.bigImage = false;
                }
            });
        }

        resultCheck = function(obj,result) {
            $scope.checkResult.result = result;
            if(result == "1") { //符合
                $(obj).css("border","1px solid #7BBFF9");
                $(obj).find("a").css("color","#6CB8F8");
                $(obj).next().css("border","1px solid #E7E7E7");
                $(obj).next().find("a").css("color","#959595");
            } else if(result == "0") {
                $(obj).css("border","1px solid #7BBFF9");
                $(obj).find("a").css("color","#6CB8F8");
                $(obj).prev().css("border","1px solid #E7E7E7");
                $(obj).prev().find("a").css("color","#959595");
            }
        }
        //
        $scope.resCheckSubmit = function() {
            if(!$scope.checkResult.files || $scope.checkResult.files.length == 0) {
                var loadi = $ionicPopup.alert({
                    cssClass : 'popup_notice',
                    template: '请先上传图片!',
                    buttons:null
                });
                $timeout(function() {
                    loadi.close(); // 3秒后关闭弹窗
                }, 1500);
                return;
            }
            $scope.checkResult.result = $('input[name="checkResult"]:checked').val();
            Page.ajaxPostQuiet(ApiEndpoint.saveCheckResultUrl, {file : $scope.checkResult.files,comment : $scope.checkResult.comment,
                result : $scope.checkResult.result,checkDetail : $scope.checkDetail}, function (resp) {

                var loadi = $ionicPopup.alert({
                    cssClass : 'popup_notice',
                    template: '提交成功!',
                    buttons:null
                });

                $timeout(function() {
                    loadi.close(); // 1秒后关闭弹窗
                    //$state.go('bus_check',null,{reload : true});

                    if(window.js != undefined && window.js.mobileAlert != undefined) {
                        window.js.mobileBack("0"); //0 返回 1退出
                    } else {
                        window.webkit.messageHandlers.mobileBack.postMessage("0");//0 返回 1退出
                    }
                    return;
                }, 1500);

                //completePopup.then(function(res) {
                //    //$state.go('bus_check',null,{reload : true});
                //
                //    if(window.js != undefined && window.js.mobileAlert != undefined) {
                //        window.js.mobileBack("0"); //0 返回 1退出
                //    } else {
                //        window.webkit.messageHandlers.mobileBack.postMessage("0")//0 返回 1退出
                //    }
                //    return;
                //});

            });
        }

        $scope.viewMediaImg = function(path) {
            $scope.Url = $rootScope.nginxPath + path;
            $scope.bigImage = true;

            var transformStyle = $("div[nav-view='active'] div.scroll").css("transform");
            var wantVal = -parseInt(transformStyle.substring(transformStyle.lastIndexOf(",") + 2,transformStyle.indexOf(")")));

            $scope.imgDivStyle = {
                "position": "absolute",
                "top": wantVal + "px",
                "left": "0",
                "z-index": "10",
                "width": "100%",
                "height": "100%"
            }
            $scope.imgStyle = {
                "position": "relative",
                "top": "5%",
                "left": "56%",
                "z-index": "10",
                "display": "block",
                "margin-top": "18px",
                "margin-left": "-165px",
                "height": "305px",
                "width": "280px",
                "box-shadow": "0 1px 3px rgba(0, 0, 0, 0.4)"
            }
        }

        $scope.hideBigImage = function () {
            $scope.bigImage = false;
        };
        $scope.indexTitle = "图片浏览";
        $scope.bigImage = false;    //初始默认大图是隐藏的
        $scope.imageIndex = -1;//当前展示的图片

        //点击图片放大
        $scope.shouBigImage = function (imageName, imageIndex) {  //传递一个参数（图片的URl）
            $scope.imageIndex = imageIndex;
            $scope.Url = imageName;                   //$scope定义一个变量Url，这里会在大图出现后再次点击隐藏大图使用
            $scope.bigImage = true;                   //显示大图
        }


        $scope.showBig = function(path,index) {
            $scope.tempIndex = index;
            $scope.Url = $rootScope.nginxPath + path;
            $scope.bigImage = true;

            var transformStyle = $("div[nav-view='active'] div.scroll").css("transform");
            var wantVal = -parseInt(transformStyle.substring(transformStyle.lastIndexOf(",") + 2,transformStyle.indexOf(")")));

            $scope.imgDivStyle = {
                "position": "absolute",
                "top": wantVal + "px",
                "left": "0",
                "z-index": "10",
                "width": "100%",
                "height": "100%"
            }
            $scope.imgStyle = {
                "position": "relative",
                "top": "5%",
                "left": "56%",
                "z-index": "10",
                "display": "block",
                "margin-top": "18px",
                "margin-left": "-165px",
                "height": "305px",
                "width": "280px",
                "box-shadow": "0 1px 3px rgba(0, 0, 0, 0.4)",
                "background-color" : "#F9F9F9"
            }
        }

}]);
